<template>
  <div>
    <!-- 位置栏 -->
    <div class="location-bar">
      <div>位置 :&ensp;</div>
      <div>一级页面</div>
      <div style="color: #000">/二级页面</div>
    </div>
    <!-- 查询框 -->
    <div class="search-bar">
      <a-form-model layout="inline" :model="searchForm" @submit="searchSubmit" @submit.native.prevent>
        <a-form-model-item label="名称:">
          <a-input v-model="searchForm.name" placeholder="请输入" />
        </a-form-model-item>
        <a-form-model-item label="创建日期:">
          <a-range-picker valueFormat="yyyy-MM-DD HH:mm:ss" v-model="searchForm.dateRange" />
        </a-form-model-item>
        <a-form-model-item>
          <a-button type="primary" html-type="submit">查询</a-button>
        </a-form-model-item>
      </a-form-model>
    </div>
    <!-- 内容框 -->
    <div class="content-box">
      <!-- 新增 -->
      <a-button type="primary" @click="add_modal"> 新增 </a-button>
      <!-- 表格 -->
      <a-table :columns="columns" :data-source="data" @change="handleTableChange" :pagination="pagination" :row-key="(record) => record.id">
        <span slot="action" slot-scope="text, record">
          <a @click="modify_modal(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="del(record)" > 删除 </a>
        </span>
      </a-table>
      <!-- 弹窗表单 -->
      <!-- 编辑 -->
      <a-modal :visible="modify_visible" title="修改友情链接" okText="确认" cancel-text="取消" @ok="modify_ok" @cancel="modify_cancel">
        <!--表单 并将表单的值绑定到this.from-->
        <a-form :form="modifyform" :label-col="{ span: 5 }" :wrapper-col="{ span: 16 }">
          <a-form-item label="名称">
            <a-input v-decorator="['title', { initialValue: modify_message.title, rules: [{ required: true, message: '请输入名称!' }] }]" />
          </a-form-item>
          <a-form-item label="链接">
            <a-input v-decorator="['linkAddress', { initialValue: modify_message.linkAddress, rules: [{ required: true, message: '请输入链接!' }] }]" />
          </a-form-item>
          <a-form-item label="排序值">
            <a-input v-decorator="['orderby', { initialValue: modify_message.orderby, rules: [{ required: true, message: '请输入排序值!' }] }]" />
          </a-form-item>
        </a-form>
      </a-modal>
      <!-- 新增 -->
      <a-modal :visible="add_visible" title="新增友情链接" okText="确认" cancel-text="取消" @ok="add_ok" @cancel="add_cancel">
        <!--表单 并将表单的值绑定到this.from-->
        <a-form :form="addform" :label-col="{ span: 5 }" :wrapper-col="{ span: 16 }">
          <a-form-item label="名称">
            <a-input v-decorator="['title', { rules: [{ required: true, message: '请输入名称!' }] }]" />
          </a-form-item>
          <a-form-item label="链接">
            <a-input v-decorator="['linkAddress', { rules: [{ required: true, message: '请输入链接!' }] }]" />
          </a-form-item>
          <a-form-item label="排序值">
            <a-input v-decorator="['orderby', { rules: [{ required: true, message: '请输入排序值!' }] }]" />
          </a-form-item>
        </a-form>
      </a-modal>
    </div>
  </div>
</template>

<script>
/**表头信息 */
const columns = [
  {
    title: '序号',
    dataIndex: 'id',
    key: 'id',
    scopedSlots: { customRender: 'id' },
  },
  {
    title: '名称',
    dataIndex: 'title',
    key: 'title',
    scopedSlots: { customRender: 'title' },
  },
  {
    title: '链接',
    dataIndex: 'linkAddress',
    key: 'linkAddress',
    scopedSlots: { customRender: 'linkAddress' },
  },
  {
    title: '排序值',
    dataIndex: 'orderby',
    key: 'orderby',
    scopedSlots: { customRender: 'orderby' },
  },
  {
    title: '创建时间',
    key: 'created',
    dataIndex: 'created',
    scopedSlots: { customRender: 'created' },
  },
  {
    title: '操作',
    key: 'action',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
  },
];
export default {
  //el 创建前
  beforeCreate() {
    //创建表单
    this.modifyform = this.$form.createForm(this, { name: 'modifyform' });
    this.addform = this.$form.createForm(this, { name: 'addform' });
  },

  data() {
    return {
      columns,
      data: [],
      searchForm: { dateRange: ['', ''], name: '' }, //查询参数
      pagination: { pageSize: 10, total: 100, pageNum: 1, showSizeChanger: true }, //分页参数
      modify_visible: false,
      modify_message: '', //被编辑信息
      modify_form: '', //编辑内容
      add_visible: false,
      add_form: '',
      del_id: '', //获取删除id
    };
  },
  created() {
    this.loadArticleList();
  },
  methods: {
    //向服务器读取文章列表
    async loadArticleList() {
      //调用服务端接口获取文章列表
      let reqData = { searchText: this.searchForm.name, startTime: this.searchForm.dateRange[0], endTime: this.searchForm.dateRange[1] };
      let reqParam = { pageNumber: this.pagination.pageNum, pageSize: this.pagination.pageSize };
      let searchUrl = `/blogroll/search/${reqParam.pageNumber}/${reqParam.pageSize}`;
      let result = await this.axios.post(searchUrl, reqData);
      console.log(result);
      this.pagination.total = result.data.total; //总数
      this.data = result.data.data;
      this.search_empty();
    },
    //向服务器新增
    async newlyAdded() {
      let reqData = { orderby: this.add_form.orderby, linkAddress: this.add_form.linkAddress, title: this.add_form.title };
      let saveUrl = `/blogroll/save`;
      let result = await this.axios.post(saveUrl, reqData);
      console.log(result);
      this.loadArticleList();
      this.addform.resetFields();
    },
    //向服务器修改列表
    async modifyList() {
      let reqData = { orderby: this.modify_form.orderby, linkAddress: this.modify_form.linkAddress, id: this.modify_form.id, title: this.modify_form.title };
      let updateUrl = `/blogroll/update`;
      let result = await this.axios.post(updateUrl, reqData);
      console.log(result);
      this.loadArticleList();
      this.modifyform.resetFields();
    },
    //向服务器删除列表
    async deleteList() {
      let reqData = {};
      let reqParam = { id: this.del_id };
      let deleteUrl = `/blogroll/delete/${reqParam.id}`;
      let result = await this.axios.delete(deleteUrl, reqData);
      console.log(result);
      this.loadArticleList();
    },
    //搜索
    searchSubmit() {
      this.loadArticleList();
      // console.log(this.searchForm)
    },
    //删除
    del(record) {
      this.del_id = record.id;
      this.deleteList();
    },
    // 分页
    handleTableChange(pagination) {
      this.pagination.pageNum = pagination.current;
      this.loadArticleList();
    },
    //显示模态框
    modify_modal(record) {
      this.modify_message = record;
      this.modify_visible = true;
    },
    //关闭模态框
    modify_cancel() {
      this.modify_visible = false;
      this.modifyform.resetFields();
    },
    //点击确认
    modify_ok() {
      const form = this.modifyform;
      form.validateFields((err, values) => {
        if (err) {
          return;
        }
        // console.log('form 表单内容',values);
        this.modify_form = values;
        this.modify_form.id = this.modify_message.id;
        console.log(this.modify_form);
        this.modify_visible = false;
        //执行修改方法
        this.modifyList();
      });
    },
    add_modal() {
      this.add_visible = true;
    },
    add_cancel() {
      this.add_visible = false;
      this.addform.resetFields();
    },
    add_ok() {
      const form = this.addform;
      form.validateFields((err, values) => {
        if (err) {
          return;
        }
        this.add_form = values;
        console.log(this.add_form);
        this.add_visible = false;
        //执行新增方法
        this.newlyAdded();
      });
    },
    //清空搜索
    search_empty() {
      this.searchForm.dateRange = ['', ''];
      this.searchForm.name = '';
    },
  },
};
</script>

<style>
.location-bar {
  display: flex;
  font-size: 14px;
  color: #88898b;
}
.search-bar {
  width: 100%;
  height: 80px;
  background-color: #ffffff;
  margin-top: 17px;
  box-sizing: border-box;
  padding: 20px 30px;
}
.content-box {
  width: 100%;
  background-color: #ffffff;
  margin-top: 20px;
  box-sizing: border-box;
  padding: 20px 30px;
}
</style>
